package com.lemon.tieba.post.web;

import com.github.pagehelper.PageInfo;
import com.lemon.common.constant.SessionContants;
import com.lemon.common.context.SessionContext;
import com.lemon.common.exception.ExceptionEnum;
import com.lemon.common.exception.BaseException;
import com.lemon.common.dto.Response;
import com.lemon.common.util.CookieUtils;
import com.lemon.tieba.post.domain.Post;
import com.lemon.tieba.post.domain.dto.Query;
import com.lemon.tieba.post.service.PostService;
import com.lemon.tieba.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 帖子 Controller
 *
 * @author ymh
 * @date 2019-3-29 16:31
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 帖子列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<PageInfo> list(Query query) {
        PageInfo<Post> pageInfo = postService.listPost(query);
        return ResponseEntity.status(HttpStatus.OK).body(pageInfo);
    }

    /**
     * 查询楼层列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list/floor")
    public ResponseEntity<PageInfo> listFloor(Query query) {
        PageInfo<Post> pageInfo = postService.listFloor(query);
        return ResponseEntity.status(HttpStatus.OK).body(pageInfo);
    }

    /**
     * 查询楼层列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list/reply")
    public ResponseEntity<PageInfo> listReply(Query query) {
        PageInfo<Post> pageInfo = postService.listReply(query);
        return ResponseEntity.status(HttpStatus.OK).body(pageInfo);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Post> get(@PathVariable Long id) {
        Post post = postService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(post);
    }

    /**
     * 新增
     *
     * @param post
     * @return
     */
    @PostMapping
    public ResponseEntity<Post> save(Post post, HttpServletRequest req) {
        // 获取 Session
        Cookie cookie = CookieUtils.getCookie(req, "JSESSIONID");
        String sessionId = cookie.getValue();
        SessionContext sessionContext = SessionContext.getInstance();
        HttpSession session = sessionContext.getSession(sessionId);
        // 从 Session 中获取当前登陆的用户
        User user = (User) session.getAttribute(SessionContants.SESSION_USER);

        post.setUserId(user.getId());
        post.setUsername(user.getNickname());
        post.setAvator(user.getAvator());
        int count = postService.save(post);
        if (count == 0) {
            throw new BaseException(ExceptionEnum.CREATE_FAILED);
        }
        return ResponseEntity.status(HttpStatus.OK).body(post);
    }

    /**
     * 修改
     *
     * @param post
     * @return
     */
    @PutMapping
    public ResponseEntity<Response> update(Post post) {
        int count = postService.update(post);
        if (count == 0) {
            throw new BaseException(ExceptionEnum.UPDATE_FAILED);
        }
        return ResponseEntity.status(HttpStatus.OK).body(new Response(200, "修改成功"));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Response> delete(@PathVariable Long id) {
        int count = postService.delete(id);
        if (count == 0) {
            throw new BaseException(ExceptionEnum.DELETE_FAILED);
        }
        return ResponseEntity.status(HttpStatus.OK).body(new Response(200, "删除成功"));
    }

}
